<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

class Highsea extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Business.Business");
        $this->ReceiveModel = model("Business.Receive");
        $this->AdminModel = model("Admin");
        $adminList = $this->AdminModel->select();
        $adminData = [];
        foreach ($adminList as $item) {
            $adminData[$item['id']] = $item['nickname'];
        }
        $busList = $this->model->where('adminid', null)->select();
        // $busData=[];
        // foreach($busList as $item)
        // {
        //     $busData[$item['id']]=$item['nickname_text'];
        // }
        $this->assign([
            'adminData' => $adminData,
            // 'busData'=>$busData,
        ]);
    }
    public function index($ids = NULL)
    {
        if ($this->request->isAjax()) {
            //接收参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['source', 'admin'])
                ->where($where)
                ->where('adminid', null)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['source', 'admin'])
                ->where($where)
                ->where('adminid', null)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            //返回json数据
            $result = ['total' => $total, 'rows' => $list];

            return json($result);
        }
        return $this->view->fetch();
    }
    // 申请
    public function apply($ids = null)
    {
        $ids = empty($ids) ? [] : explode(',', $ids);

        $row = $this->model->column('id');
        $data = [];
        $list = [];
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('No results were found'));
                exit;
            }
            ;
            $data[] = [
                'id' => $item,
                'adminid' => $this->auth->id
            ];
            $list[] = [
                'applyid' => $this->auth->id,
                'status' => 'apply',
                'busid' => $item
            ];
        }
        // 开启事务
        $this->model->startTrans();
        $this->ReceiveModel->startTrans();

        $BuStatus = $this->model->isUpdate(true)->saveAll($data);
        if ($BuStatus === FALSE) {
            $this->model->rollback();
            $this->error($this->model->getError());
            exit;
        }
        $reStatus = $this->ReceiveModel->saveAll($list);
        if ($reStatus === FALSE) {
            $this->ReceiveModel->rollback();
            $this->error($this->ReceiveModel->getError());
        }
        if ($BuStatus === FALSE || $reStatus === FALSE) {
            // 事务回滚
            $this->model->rollback();
            $this->ReceiveModel->rollback();
            $this->error('申请失败');
            exit;
        } else {
            $this->model->commit();
            $this->ReceiveModel->commit();
            $this->success('申请成功');
            exit;
        }
    }
    // 分配
    public function allot($ids = null)
    {
        $ids = empty($ids) ? [] : explode(',', $ids);
        $busData = $this->model->select($ids);

        if (!$busData) {
            $this->error(__('No Results were found'));
            exit;
        }
        $this->assign([
            'busData' => $busData
        ]);
        if ($this->request->isPost()) {
            $params = $this->request->param('row/a');
            $data = [];
            $list = [];
            foreach ($busData as $item) {
                $data[] = [
                    'id' => $item['id'],
                    'adminid' => $params['adminid']
                ];
                $list[] = [
                    'applyid' => $params['adminid'],
                    'status' => 'allot',
                    'busid' => $item['id']
                ];
            }
            // 开启事务
            $this->model->startTrans();
            $this->ReceiveModel->startTrans();

            $BuStatus = $this->model->isUpdate(true)->saveAll($data);
            if ($BuStatus === FALSE) {
                $this->model->rollback();
                $this->error($this->model->getError());
                exit;
            }
            $reStatus = $this->ReceiveModel->saveAll($list);
            if ($reStatus === FALSE) {
                $this->ReceiveModel->rollback();
                $this->error($this->ReceiveModel->getError());
            }
            if ($BuStatus === FALSE || $reStatus === FALSE) {
                // 事务回滚
                $this->model->rollback();
                $this->ReceiveModel->rollback();
                $this->error('分配失败');
                exit;
            } else {
                $this->model->commit();
                $this->ReceiveModel->commit();
                $this->success('分配成功');
                exit;
            }
        }
        return $this->view->fetch();
    }
    public function del($ids = null)
    {
        $ids = empty($ids) ? [] : explode(',', $ids);
        $busData = $this->model->select($ids);

        if (!$busData) {
            $this->error(__('No Results were found'));
            exit;
        }
        $result=$this->model->destroy($ids);
        if($result=== FALSE)
        {
            $this->error($this->model->getError());
            exit;
        }else
        {
            $this->success();
            exit;
        }
    }
}